/**
 * Created by Administrator on 2015/9/8.
 */

$(document).ready(function(){
    $(document).on('click','.show',function(e){
        var a= $(e.target).text();
        if(a=="回复") {
            $(e.target).parent().next().css({"display": "block"});
            $(e.target).parent().css({"border-bottom": "none"});
            $(e.target).text("取消");
        }
        else if(a=="取消") {
            $(e.target).parent().next().css({"display": "none"});
            $(e.target).text("回复")
        }

    });


   $(document).on("click",'#submit',function(){
       if( $("#context").val()=='')
       {
           alert("不能为空");
       }
       else $.ajax({
           type: "POST",
           url: "http://localhost/yii/web/index.php?r=comment/send",
           data: { context: $("#context").val(),pid:"0"},
           success: function (data) {
               $("#context").val("");
               var data=eval("("+data+")");
               var s="<ul class='comment' ><div class='comment-name'>"+data.username+""+data.time+"</div><div class='context'>"+data.context+"</div> <div class='submit-box'><a  class='show' >回复</a></div><div class='answer'><textarea class='form-control answer-context'></textarea><br><button class='btn btn-primary answer-submit' data-pid="+data.id+" data-username="+data.username+">提交</button></div>";
               $("#comment-list-box").append(s);
          //   window.location.reload();



           }
       });

    });

    $(document).on("click",'.answer-submit',function(e){
       var pid=$(e.target).data("pid");
        var context=$(e.target).prev().prev().val();
        var username=$(e.target).data("username");
        var mark=$(e.target).parent().parent();

       $.ajax({
           type: "POST",
           url: "http://localhost/yii/web/index.php?r=comment/answer",
           data: { context:context,pid:pid,username:username},
           success: function (data) {
               var data=eval("("+data+")");
               $(".answer-context").val("");
              var s="<ul class='comment' ><div class='comment-name'>"+data.username+""+data.time+"</div><div class='context'>"+data.context+"</div> <div class='submit-box'><a  class='show' >回复</a></div><div class='answer'><textarea class='form-control answer-context'></textarea><br><button class='btn btn-primary answer-submit' data-pid="+data.id+" data-username="+data.username+">提交</button></div>";


               mark.append(s);



           // window.location.reload();

           }

    });
});

});
